package com.wuqi.dev.logindemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String URL_REGISTER = "http://172.16.16.84:8080/v1/servlet/register";
    private static final String URL_LOGIN = "http://172.16.16.84:8080/v1/servlet/login";
    private static final String URL_GET_INFO = "http://172.16.16.84:8080/v1/servlet/getInfo";
    private TextView textShow;
    private OkHttpClient mClient;

    private String token;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textShow = (TextView) findViewById(R.id.text_show);
        mClient = new OkHttpClient.Builder()
                // 添加HTTP请求的拦截器
                .addInterceptor(new TokenInterceptor(this))
                .build();
    }

    public void register (View view) {
        // POST请求, 在请求体里发送三个字段
        // 1.username
        // 2.email
        // 3.password
        Observable.just(URL_REGISTER)
                .map(new Function<String, String>() {
                    @Override
                    public String apply (@NonNull String s) throws Exception {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("username", "qwert")
                                .add("email", "40184098@qq.com")
                                .add("password", "qwer4321")
                                .build();
                        Request request = new Request.Builder()
                                .url(s)
                                .post(requestBody)
                                .build();
                        Response response = mClient.newCall(request).execute();
                        return response.body().string();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept (@NonNull String s) throws Exception {
                        textShow.setText(s);
                    }
                });



    }


    public void login (View view) {
        Observable.just(URL_LOGIN)
                .map(new Function<String, String>() {
                    @Override
                    public String apply (@NonNull String s) throws Exception {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("username", "qwert")
                                .add("password", "qwer4321")
                                .build();
                        Request request = new Request.Builder()
                                .url(s)
                                .post(requestBody)
                                .build();
                        Response response = mClient.newCall(request).execute();
                        return response.body().string();
                    }
                })
                .map(new Function<String, String>() {
                    @Override
                    public String apply (@NonNull String s) throws Exception {
                        return new JSONObject(s).getString("token");
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept (@NonNull String s) throws Exception {
                        textShow.setText(s);
                        TokenUtil.saveToken(MainActivity.this, s);
                    }
                });
    }

    public void getInfo (View view) {
        Observable.just(URL_GET_INFO)
                .map(new Function<String, String>() {
                    @Override
                    public String apply (@NonNull String s) throws Exception {
                        Request request = new Request.Builder()
                                .url(s)
                                .build();
                        Response response = mClient.newCall(request).execute();
                        return response.body().string();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept (@NonNull String s) throws Exception {
                        textShow.setText(s);
                    }
                });
    }
}
